from django.db import models
from django.contrib.auth import get_user_model

# python manage.py makemigrations
#python manage.py migrate

User = get_user_model()


class Text(models.Model):
    """文本模型"""
    content = models.TextField(verbose_name='文本内容')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')


class Tag(models.Model):
    """标签模型"""
    name = models.CharField(max_length=50, unique=True, verbose_name='标签名称')
    creator = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        null=True,
        related_name='created_tags',
        verbose_name='创建者'
    )
    related_texts = models.ManyToManyField(
        Text,
        through='Tagging',
        related_name='tags',
        verbose_name='关联文本'
    )
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')


class Tagging(models.Model):
    """标签与文本的关联模型"""
    tag = models.ForeignKey(
        Tag,
        on_delete=models.CASCADE,
        verbose_name='标签'
    )
    text = models.ForeignKey(
        Text,
        on_delete=models.CASCADE,
        verbose_name='关联文本'
    )
    user = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        null=True,
        related_name='tagged_texts',
        verbose_name='关联用户'
    )
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='关联时间')